<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>自定义指令</title>
    <script type='text/javascript' src="../js/vue.js"></script>
</head>

<body>
    <div id="root">
        <h2>当前的n值是{{n}}</h2>
        <h2>放大10倍后的n值是：<span v-big="n"></span></h2>
        <button @click="n++">点我n+1</button>
        <hr />
        <input type="text" v-fbind:value="n" />
    </div>
</body>
<script type='text/javascript'>
    new Vue({
        el: '#root',
        data: {
            n: 1
        },
        directives: {
            //element 是指令所在标签， binding 包含指令相关信息
            big(element, binding) {
                element.innerText = binding.value * 10
            },
            //定义一个fbind指令
            fbind: {
                //指令与元素成功绑定时执行（初始化）
                bind(element, binding) {
                    element.value = binding.value;
                },
                //指令所在元素被插入页面时
                inserted(element, binding) {
                    element.focus()
                },
                //指令所在的模板被重新解析时
                update(element, binding) {
                    element.value = binding.value;
                }
            }
        }

    })
</script>

</html>